﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BonsaiScrum.Bus;

namespace BonsaiScrum.Core.BusImpl
{
    public class InMemoryQueue : IQueue
    {
        private readonly Queue<IMessage> _messageQueue;
        private readonly Queue<Action<IMessage>> _listenerQueue;

        public InMemoryQueue()
        {
            _messageQueue = new Queue<IMessage>(32);
            _listenerQueue = new Queue<Action<IMessage>>(32);
        }

        public void Put(IMessage message)
        {
            if (_listenerQueue.Count == 0)
            {
                _messageQueue.Enqueue(message);
                return;
            }

            var listener = _listenerQueue.Dequeue();
            listener(message);
        }

        public void Pop(Action<IMessage> popAction)
        {
            if (_messageQueue.Count == 0)
            {
                _listenerQueue.Enqueue(popAction);
                return;
            }

            var message = _messageQueue.Dequeue();
            popAction(message);
        }
    }
}
